<?php
namespace Admin\Help;
//返回数据类
use Admin\AppConst\WebConst;

class ReturnMessage{
    /**
     * @var ReturnMessage
     */
    private static $instance;

    /**
     * Get the instance of BannerService.
     * @return ReturnMessage
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new ReturnMessage();
        }
        return self::$instance;
    }
    /* @function    success         成功返回参数
     * @param       $data           array   数据
     * @param       $messageCode    int     提示语编号
     * @date        2018-07-30
     * @author      zhangle
     */
    public function success($data, $messageCode = WebConst::DEFAULT_TYPE_MESSAGE_CODE){
        $this->format($data, WebConst::DEFAULT_TYPE_SUCCESS, $messageCode);
    }

    /* @function    error   失败返回
     * @date        2018-07-30
     * @author      zhangle
     */
    public function error($messageCode){
        $this->format(array(), WebConst::DEFAULT_TYPE_SUCCESS, $messageCode);
    }

    /* @function    alert   提示返回参数
     * @date        2018-07-30
     * @author      zhangle
     */
    public function alert($messageCode){
        $this->format(array(), WebConst::DEFAULT_TYPE_ALERT, $messageCode);
    }

    /* @function    exception   提示返回参数
     * @date        2018-07-30
     * @author      zhangle
     */
    public function exception($messageCode){
        $this->format(array(), WebConst::DEFAULT_TYPE_EXCEPTION, $messageCode);
    }

    /* @function apiResultFormat() api接口返回数据格式化
     * @param  $data        array   需要返回的数据
     * @param  $code        int     错误码类型
     * @param  $msgCode     int     错误码
     * @param  $isShowData  bool    是否显示数据
     * @date   2018-07-26
     * @author zhangle
     * @return string
     */
    function format($data = WebConst::DEFAULT_TYPE_EMPTY_ARRAY, $code = WebConst::DEFAULT_TYPE_STATUS_TRUE, $msgCode = WebConst::DEFAULT_TYPE_MESSAGE_CODE,$isShowData = WebConst::DEFAULT_TYPE_TRUE){

        $result['code']             = $code;
        $result['msg']              = $msgCode.': '.C('appMessage')[$msgCode];
        $result['$nowTimestamp']    = time();
        $result['$lastPublishTime'] = time();
        if($isShowData == WebConst::DEFAULT_TYPE_TRUE){
            if(array_keys($data)[0] == 0){
                $data   = array('list' => $data);
            }
            $result['data'] = $data;
        }
        $result = json_encode($result, JSON_UNESCAPED_UNICODE);
        echo $result;
        exit;
    }
}
